<?php

/**
 * 说明：
 * User: wenqiang.yq@gmail.com
 * Date: 16-3-7
 * Time: 上午9:08
 */

include_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "ElasticSearch.php";
define("hosts", "http://127.0.0.1:9200");
define("index", "my_thread_t2");
define("type", "forums");
define("limit", 100);

//建立话题索引
function createIndex()
{
    $es = new \ElasticSearch\ElasticSearch(hosts);
    $setting = array(
        "index" => array(
            "analysis" => array(
                "analyzer" => array( //分析器
                    "pinyin_analyzer" => array(
                        "tokenizer" => "pinyin",
                        "filter" => array("word_delimiter", "nGram")
                    )
                ),
                "filter" => array(), //过滤器

                "tokenizer" => array(  //分词器
                    "pinyin" => array(
                        "type" => "pinyin",
                        "first_letter" => "append",
                        "padding_char" => " "
                    )
                )
            )
        )
    );
    $mapping = array(
        type => array(
            "properties" => array(
                "tid" => array(
                    "type" => "integer",
                    "index" => "not_analyzed"
                ),
                "subject" => array(
                    "type" => "multi_field",
                    "fields" => array(
                        "raw" => array(
                            "type" => "string",
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ),
                        "py" => array(
                            "type" => "string",
                            "term_vector" => "with_positions_offsets",
                            "analyzer" => "pinyin_analyzer"
                        )
                    )
                ),
                "message" => array(
                    "type" => "string",
                    "analyzer" => "ik_max_word",
                    "search_analyzer" => "ik_max_word"
                ),
                "tagid" => array(
                    "type" => "integer",
                    "index" => "not_analyzed"
                ),
                "nick" => array(
                    "type" => "string",
                    "analyzer" => "ik_max_word",
                    "search_analyzer" => "ik_max_word"
                ),
                "lastauthorid" => array(
                    "type" => "integer",
                    "index" => "not_analyzed"
                ),
                "kind_id" => array(
                    "type" => "integer",
                    "index" => "not_analyzed"
                ),
                "dateline" => array(
                    "type" => "date",
                    "store" => true,
                    "index" => "not_analyzed"
                ),
                "lastpost" => array(
                    "type" => "date",
                    "store" => true,
                    "index" => "not_analyzed"
                )
            )
        )
    );
    $result = $es->index()->create(index, 4, 2, $mapping, $setting);
    print_r($result);
}

/**
 * my_thread forums的结构
 * tid：int
 * subject string
 * tagid int
 * nick string
 * lastauthorid int
 * kind_id int
 * dateline int
 * lastpost int
 * message string
 */


//将数据库数据导入es
function main()
{
    $start = 100;
    $threads = thread($start);
    $count = 0;
    while ($threads) {
        $bulkData = [];
        foreach ($threads as $thread) {
            $bulkData[] = array(
                "id" => $thread['tid'],
                "tid" => $thread['tid'],
                "subject" => $thread['subject'],
                "message" => $thread['message'],
                "tagid" => $thread['tagid'],
                "kind_id" => $thread['kind_id'],
                "nick" => $thread['nick'],
                "lastauthorid" => $thread['lastauthorid'],
                "dateline" => $thread['dateline'],
                "lastpost" => $thread['lastpost']
            );
        }
        $count += count($bulkData);
        $es = new \ElasticSearch\ElasticSearch(hosts, index, type);
        $es->data()->add($bulkData);
        $start = $start + limit;
        $threads = thread($start);
    }
    echo "总共导入了{$count}条数据\n";
}


//获取话题信息
function thread($start = 100)
{
    $datas = fetchThread($start);
    $tables = [];
    $base = 30000000;
    foreach ($datas as $data) {
        $i = intval(ceil(($data['tid'] - $base) / 2000000)); //200万数据分一张表
        $tables[$i][] = $data['pid'];
    }
    foreach ($tables as $key => $value) {
        $replys = fetchReply("forums_post_{$key}", implode(",", $value));
        foreach ($replys as $reply) {
            $datas[$reply["tid"]]['message'] = $reply["message"];
        }
    }
    return $datas;
}

function query($sql)
{
    $db = new mysqli("192.168.51.216", "chaodong", "chaodong4399", "uchome");
    $db->set_charset("utf8");
    $result = $db->query($sql);
    return $result;
}

function fetchThread($start = 0)
{
    $result = query("select * " . "from forums_thread order by tid asc limit {$start}," . limit);
    $datas = [];
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $datas[$row["tid"]] = $row;
    }
    return $datas;

}

function fetchReply($table, $pids)
{
    $result = query("select * " . "from {$table} where pid in ({$pids})");
    $datas = [];
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $datas[$row["tid"]] = $row;
    }
    return $datas;
}

//测试搜索
function search()
{

    $es = new \ElasticSearch\ElasticSearch(hosts);
    $result = $es->search()->bodyQuery('{
  "query": {
    "match": {
      "subject": "ceshi"
    }
  }
  }'
    );
    print_r($result->result());
}

createIndex();
main();
